package com.boxin.ims.modules.wechat.busi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.SeckillGainer;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.SeckillGainerService;

/**
 * @author Jakemanse
 * @date 2013-10-12
 * @function <p>查询我的秒杀 </p>
 */
@Service
public class SeckillQueryProcessor extends BusinessProcessor{
	@Autowired
	SeckillGainerService seckillGainerService;
	
	public SeckillQueryProcessor() {
	}

	@Override
	public ProcessMessage process() {
		String reply = "亲,对不起,未找到您秒杀中奖的信息,谢谢关注！";
		WechatMember member = this.memberCheckAndReply();
		if(member!=null){
			SeckillGainer seckillGainer = new SeckillGainer();
			seckillGainer.setMember(member);
			seckillGainer.setUser(user);
			seckillGainer.setStatus(seckillGainer.STATUS_NORMAL);
			
			List<SeckillGainer> gainers = 	seckillGainerService.find(seckillGainer);
			
			if(gainers!=null &&!gainers.isEmpty()){	//有中奖信息
				if(gainers.size() == 1){	//只有一条秒杀中奖信息
					SeckillGainer sgn = gainers.get(0);
					String title = "您有未使用的奖品【" + sgn.getName()
							+ "】";
					Date date = sgn.getEndTime();
					String validDate = DateUtils.formatDate(
							(date == null) ? new Date() : date, "MM月dd日");
					String desc = "亲，您有一份未使用的奖品【" +sgn.getName() + "】,请于"
							+ validDate + "之前到店里凭此二维码核销使用，过期作废，谢谢！";
					
					writeSingleLotteryImageToUser(title, desc,
							sgn.getQrCode().getId());
					
					return null;
					
				}else{
					List<NewsReply> newsReplies = new ArrayList<NewsReply>();
					for(SeckillGainer gn : gainers){
						String title = "您有未使用的奖品【" + gn.getName()	+ "】";
						Date date = gn.getEndTime();
						String validDate = DateUtils.formatDate((date == null) ? new Date() : date, "MM月dd日");
						String desc = "亲，您有一份未使用的奖品【" +gn.getName() + "】,请于"+ validDate + "之前到店里凭此二维码核销使用，过期作废，谢谢！";
						
						NewsReply newsReply = new NewsReply();
						newsReply.setTitle(title);
						newsReply.setDescription(desc);
						newsReply.setPicUrl(QRCodeUtils.getQRCodePNGURL(gn.getQrCode().getId()));
						newsReply.setUrl(QRCodeUtils.getQRCodePNGURL(gn.getQrCode().getId()));
						
						newsReplies.add(newsReply);
					}
					
					writeQueryLotteryImageToUser(newsReplies);
					return null;
				}
			} 
			
			writeToUser(reply);
		}
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		// TODO Auto-generated method stub
		return false;
	}
	
	

}
